package com.lune.job.controller;


import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.pojo.Channel;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IChannelService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "栏目管理")
public class ChannelController {

    @Autowired
    private IChannelService channelService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "添加栏目")
    @PostMapping("/")
    public RespBean create(@RequestBody Channel channel) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        channel.setCreate_date(new Date());
        channel.setCreate_user(userData.getId());
        if (channel.getParent_id() == null) {
            channel.setParent_id(0);
        }
        boolean flag = channelService.save(channel);
        if (flag) {
            return RespBean.success("添加栏目成功");
        } else {
            return RespBean.error("添加栏目失败");
        }

    }
    @ApiOperation(value = "删除栏目")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable  Integer id){
        if (channelService.removeById(id)){
            return RespBean.success("删除栏目成功");
        } else {
            return RespBean.error("删除栏目失败");
        }
    }

    @ApiOperation(value = "批量删除栏目")
    @DeleteMapping("/many")
    public RespBean delete(Integer[] ids) {
        boolean flag = channelService.removeByIds(Arrays.asList(ids));
        if (flag) {
            return RespBean.success("批量删除栏目成功");
        } else {
            return RespBean.error("批量删除栏目失败");
        }
    }

    @ApiOperation(value = "更新栏目")
    @PutMapping("/")
    public RespBean update(@RequestBody Channel channel) {
        boolean flag = channelService.updateById(channel);
        if (flag) {
            return RespBean.success("更新栏目成功");
        } else {
            return RespBean.error("更新栏目失败");
        }

    }

    @ApiOperation(value = "根据id查询栏目")
    @GetMapping("/detail")
    public RespBean detail(Integer id) {
        Channel byId = channelService.getById(id);
        if (byId != null) {
            return RespBean.success("查询栏目成功", byId);
        } else {
            return RespBean.error("查询栏目失败");
        }
    }

    @ApiOperation(value = "分页查询栏目")
    @GetMapping("/queryPage")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Channel channel) {
        return channelService.queryPage(currentPage, size, channel);
    }

    @ApiOperation(value = "条件查询栏目")
    @PostMapping("/queryByCon")
    public List<Channel> queryByCon(@RequestBody Channel channel) {
        return channelService.queryByCon(channel);
    }

    @GetMapping("/tree")
    public RespBean tree() {
        List<Channel> list = channelService.tree(null);
        List<Map<String, Object>> mapList = new ArrayList<>();
        list.forEach(channel -> {
            if (channel.getParent_id() == 0) {
                Map map = new HashMap();
                map.put("id", channel.getId());
                map.put("label", channel.getName());
                if (isChildren(channel.getId(), list)) {
                    map.put("children", children(channel.getId(), list));
                }
                mapList.add(map);
            }
        });
        return RespBean.success("树形查询成功", mapList);
    }

    //如何支持多级：递归【自己调用自己、出口】
    public List<Map<String, Object>> children(int id, List<Channel> list) {
        List<Map<String, Object>> children = new ArrayList<>();
        for (Channel channel : list) {
            if (channel.getParent_id() == id) {
                Map map = new HashMap();
                map.put("id", channel.getId());
                map.put("label", channel.getName());
                if (isChildren(channel.getId(), list)) {//出口
                    map.put("children", children(channel.getId(), list));
                }
                children.add(map);
            }
        }
        return children;
    }

    public boolean isChildren(int id, List<Channel> list) {
        boolean flag = false;
        for (Channel channel : list) {
            if (channel.getParent_id() == id) {
                flag = true;
            }
        }
        return flag;
    }


}
